<%--
  Created by IntelliJ IDEA.
  User: 10330
  Date: 2021/7/6
  Time: 12:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="modal fade" id="loginFormModal" role="dialog" aria-hidden="true" aria-labelledby="myModalLabel">
    <form role="form" action="${pageContext.request.contextPath}/home?action=index" method="post">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" aria-hidden="true" type="button"
                            data-dismiss="modal">×</button>
                    <h4 class="modal-title" id="myModalLabel" style="text-align: left">用户登录</h4>
                </div>
                <div class="modal-body">

                    <div class="form-group" style="text-align: left">
                        <label for="username">用户名</label><input type="text" class="form-control" id="username" />
                    </div>
                    <div class="form-group" style="text-align: left">
                        <label for="password">密码</label><input type="password" class="form-control" id="password" />
                    </div>


                </div>
                <div class="modal-footer">

                    <button class="btn btn-primary" type="button" onclick="login()">登录</button>
                    <button class="btn btn-default" type="button" data-dismiss="modal">关闭窗口</button>

                </div>
            </div>
        </div>
    </form>
</div>

<script>

    function login(){
        var username=$("#username").val();
        var password=$("#password").val();

        $.post("${pageContext.request.contextPath}/home?action=login",{username:username,password:password},
            function(result){

                if(result.login===true){
                    window.location.href="${pageContext.request.contextPath}/home?action=index";
                }
                else{
                    $("#msgTitle").html("登录失败");
                    $("#msgBody").html("用户名或密码错误");
                    $("#msgModal").modal();
                }

            },"json");
    }

    function logout(){
        window.location.href="${pageContext.request.contextPath}/home?action=logout";
    }

</script>